package org.jeecg.modules.product.risk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.product.risk.entity.TbBaseMeasuresTaskRecord;
import org.jeecg.modules.product.risk.service.ITbBaseMeasuresTaskRecordService;
import org.jeecg.modules.product.riskUnit.entity.TbBaseRiskDevice;
import org.jeecg.modules.product.riskUnit.entity.TbBaseRiskUnit;
import org.jeecg.modules.product.riskUnit.service.ITbBaseRiskDeviceService;
import org.jeecg.modules.product.riskUnit.service.ITbBaseRiskUnitService;
import org.jeecg.modules.product.riskUnit.vo.TbBaseRiskDevicePage;
import org.jeecg.modules.product.until.BaseRiskCommonConstant;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: 重大危险源装置关联风险排查任务控制层
 * @Version: V1.0
 */
@Tag(name="重大危险源装置关联风险排查任务控制层")
@RestController
@RequestMapping("/TbBaseRiskDeviceForTaskRecord/tbBaseRiskDeviceForTaskRecord")
@Slf4j
public class TbBaseRiskDeviceForTaskRecordController {
	@Autowired
	private ITbBaseRiskDeviceService tbBaseRiskDeviceService;
	@Autowired
	private ITbBaseMeasuresTaskRecordService tbBaseMeasuresTaskRecordService;
	
	/**
	 * 分页列表统计任务数根据重大危险源
	 *
	 * @param tbBaseRiskDevice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "重大危险源装置关联风险排查任务控制层-分页列表统计任务数根据重大危险源")
	@Operation(summary="重大危险源装置关联风险排查任务控制层-分页列表统计任务数根据重大危险源")
	@GetMapping(value = "/countListByRiskDevice")
	public Result<IPage<TbBaseRiskDevice>> countListByRiskDevice(TbBaseRiskDevice tbBaseRiskDevice,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 @RequestParam(name="riskDeviceId",required = false) String riskDeviceId,
														 @RequestParam(name="startDate",required = false) String startDate,
														 @RequestParam(name="endDate",required = false) String endDate,
														 HttpServletRequest req) {
		QueryWrapper<TbBaseRiskDevice> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseRiskDevice, req.getParameterMap());
		Page<TbBaseRiskDevice> page = new Page<TbBaseRiskDevice>(pageNo, pageSize);
		queryWrapper.eq("deleted", CommonConstant.DEL_FLAG_0.toString());
		queryWrapper.in("serious_hazard","serious_hazard01","serious_hazard02","serious_hazard03","serious_hazard04");
		if (StringUtils.isNotBlank(riskDeviceId)){
			queryWrapper.eq("id",riskDeviceId);
		}
		IPage<TbBaseRiskDevice> pageList = tbBaseRiskDeviceService.page(page, queryWrapper);

		List<Map> mapList = tbBaseMeasuresTaskRecordService.countByRiskDeviceAndCheckStatus(startDate,endDate,riskDeviceId);
		List<TbBaseRiskDevice> deviceList = pageList.getRecords();
		if (!deviceList.isEmpty()){
			for(TbBaseRiskDevice device : deviceList){
				int mainTaskSum = 0;
				int mainWaitCount = 0;
				int mainDueCount = 0;
				int technologyTaskSum = 0;
				int technologyWaitCount = 0;
				int technologyDueCount = 0;
				int operateTaskSum = 0;
				int operateWaitCount = 0;
				int operateDueCount = 0;
				for(Map map : mapList) {
					String risk_device_id = map.get("risk_device_id").toString();
					int count = Integer.parseInt(map.get("count").toString());
					if (device.getId().equals(risk_device_id)) {
						String work_type = map.get("work_type").toString();
						String check_status = map.get("check_status").toString();

						if(BaseRiskCommonConstant.GUARANTEELIABILITY_MAIN.equals(work_type)){
							mainTaskSum = mainTaskSum + count;
                            if(BaseRiskCommonConstant.CHECK_STATUS_NO_INVESTIGATED.equals(check_status)){
                                mainWaitCount = mainWaitCount + count;
                            } else if (BaseRiskCommonConstant.CHECK_STATUS_TIME_OUR.equals(check_status)) {
                                mainDueCount = mainDueCount + count;
                            }
						} else if (BaseRiskCommonConstant.GUARANTEELIABILITY_TECHNOLOGY.equals(work_type)) {
							technologyTaskSum = technologyTaskSum + count;
							if(BaseRiskCommonConstant.CHECK_STATUS_NO_INVESTIGATED.equals(check_status)){
								technologyWaitCount = technologyWaitCount + count;
							} else if (BaseRiskCommonConstant.CHECK_STATUS_TIME_OUR.equals(check_status)) {
								technologyDueCount = technologyDueCount + count;
							}
						} else if (BaseRiskCommonConstant.GUARANTEELIABILITY_OPERATE.equals(work_type)) {
							operateTaskSum = operateTaskSum + count;
							if(BaseRiskCommonConstant.CHECK_STATUS_NO_INVESTIGATED.equals(check_status)){
								operateWaitCount = operateWaitCount + count;
							} else if (BaseRiskCommonConstant.CHECK_STATUS_TIME_OUR.equals(check_status)) {
								operateDueCount = operateDueCount + count;
							}
						}
					}
				}
				device.setMainTaskSum(mainTaskSum);
				device.setMainWaitCount(mainWaitCount);
				device.setMainDueCount(mainDueCount);
				device.setTechnologyTaskSum(technologyTaskSum);
				device.setTechnologyWaitCount(technologyWaitCount);
				device.setTechnologyDueCount(technologyDueCount);
				device.setOperateTaskSum(operateTaskSum);
				device.setOperateWaitCount(operateWaitCount);
				device.setOperateDueCount(operateDueCount);
			}
		}

		return Result.OK(pageList);
	}

}
